<?php
class Orders extends  Controller
{
    public $userdata;
    function Orders()
    {
        parent::Controller();
      	$this->userdata = checksession($this->session,"systemdata");
		header("Content-Type: text/html; charset=utf-8");
		$this->db->query("SET time_zone = '+8:00'");
		
    }
    
    
    function lists()
    {
    	
    	$query = $this->db->query("select * from ".table("orders")." as o left join ".table("food")." as f on o.foodid=f.foodid left join ".table("eatery")." as e on f.eatid=e.eateryid left join ".table("users")." as u on o.userid=u.userid  where o.companyid=".$this->userdata["companyid"]." and o.isdo=0 and date(o.createtime)='".date("Y-m-d")."'");
    	
    	$rs = $query->result_array();
    	if(is_array($rs))
    	{
    		$result = array();
    		$company = array();
    		$foodlist = array();
    		$orders_array = array();
    		foreach ($rs as $v)
    		{
    			@$result[$v["eatid"]][$v["foodid"]]["num"]+= $v["foodnum"];
    			$result[$v["eatid"]][$v["foodid"]]["price"] = $v["price"];
    			$result[$v["eatid"]][$v["foodid"]]["foodname"] =  $v["foodname"];
    		    $company[$v["eatid"]] = array("start"=>$v["sendstart"],"cost"=>$v["sendcost"],"name"=>$v["eatname"]."(".$v["tel"].")");
    		    $foodlist[$v["eatid"]][] = array("user"=>$v["nickname"],"food"=>$v["foodname"],"num"=>$v["foodnum"]);
    		    
    		    $orders_array[] = $v["ordersid"];
    		}
            
    		$display = array();
    		foreach ($result as $key=>$va)
    		{
    			$info = $price  = $return = array();
    			foreach ($va as $d)
    			{
    				$info[] = $d["foodname"].":<font color='red'>".$d["num"]."份</font>";
    				$price[] = $d["price"]*$d["num"];
    			}
    			$return["info"] = implode("<br />",$info);
    			$return["price"] = array_sum($price);
    			$return["company"] = $company[$key]["name"];
    			
    			$return["remark"] = "起送金额:".$company[$key]["start"]."元<br />配送费".$company[$key]["cost"]."元";
    			
    			$return["child"] = $foodlist[$key];
    			
    			$display[] = $return;
    		}
    		
    		$this->mytp->assign("info",$display);
    		$this->mytp->assign("ordersid",base64_encode(serialize($orders_array)));
    	}
    	
    	
    	$this->mytp->assign("full_page",1);
    	$this->mytp->assign("page_title","查看订单");
    	$this->mytp->tp("orders_list");
		 
    }
    function team()
    {
    	
    	
    	$query = $this->db->query("select userid,nickname from ".table("users")." where companyid=".$this->userdata["companyid"]);
    	$rs = $query->result_array();
    	
    	foreach ($rs as $v)
    	{
    		$result[$v["userid"]."^".$v["nickname"]] = $v["nickname"];
    	}
    	$this->mytp->assign("page_title","团体订餐");
    	$this->mytp->assign("users",$result);
    	$this->mytp->tp("orders_team");
    	
    	
    }
    function saveteam()
    {
    	
    	$users = $_POST["users"];
    	$price = $_POST["price"];
    	
    	$orders_num = $_POST["orders_num"];
    	
    	if(count($users)>0 && $price>0)
    	{
    		$result =  array();
    		foreach ($users as $u)
    		{
    			$_users = explode("^", $u);
    			
    			$result[$_users[0]] = $_users[1];
    			
    		}
    		$users = $result;
    		$per_price = round($price/(count($users)));
    		
    	}
    	$this->mytp->assign("page_title","团体订餐统计");
    	$this->mytp->assign("users",$users);
    	$this->mytp->assign("price",$price);
    	$this->mytp->assign("orders_num",$orders_num);
    	$this->mytp->assign("per_price",$per_price);
    	$this->mytp->tp("orders_save_team");
    	
    	
    }
    function saveteam_statistics()
    {
    	$users = $_POST["users"];
    	$price = $_POST["price"];
    	$orders_num = $_POST["orders_num"];
    	$per_price = $_POST["per_price"];
    	if(count($users)>0)
    	{
    		$this->db->query("INSERT INTO ".table("order_team")."(
																`price` ,
																`per_price` ,
																`orders_num` ,
																`orders_person` ,
																`orders_time` 
																)
																VALUES (
																 '".$price."', '".$per_price."', '".$orders_num."', '".count($users)."', '".date("Y-m-d H:i:s")."')");
    		$order_team_id = $this->db->insert_id();
    		$insert = "INSERT INTO ".table("orders")."(
													userid ,
													companyid,
													foodid ,
													foodnum,
													foodprice,
													isdo,
													is_team
													)
													VALUES ";
    		$rowtpl = "(%s,%s,%s,%s,%s,1,1),";
    		$rows = "";
    		foreach ($users as $v)
    		{
    			$this->db->query("update ".table("users")." set balance=balance-".$per_price." where userid=".$v);
    			$rows.=sprintf($rowtpl,$v,$this->userdata["companyid"],$order_team_id,1,$per_price);
    			
    		}
    		$insert.=substr($rows,0,-1);
    		$this->db->query($insert);
    		
    	}
    	s("处理成功",baseurl."orders/team/");
    	
    	
    	
    }
    function addprice()
    {
    	
    	$query = $this->db->query("select userid,nickname from ".table("users")." where companyid=".$this->userdata["companyid"]);
    	$rs = $query->result_array();
    	$result = array("请选择");
    	foreach ($rs as $v)
    	{
    		$result[$v["userid"]] = $v["nickname"];
    	}
    	
    	
    	$this->mytp->assign("page_title","充值");
    	$this->mytp->assign("users",$result);
    	$this->mytp->tp("price_add");
    }
    function saveprice()
    {
    	$users = $_POST["users"];
    	$price = $_POST["price"];
    	if($users>0)
    	{
    		$this->db->query("insert into ".table("topup")."(fid,companyid,userid,price) values(".$this->userdata["userid"].",".$this->userdata["companyid"].",".$users.",".$price.")");
    		$this->db->query("update ".table("users")." set balance=balance+".$price." where userid=".$users);
    		s("充值成功",baseurl."orders/pricelist/");
    	}
    }
    function pricelist()
    {
    	 $is_ajax = empty($_REQUEST["is_ajax"])?0:$_REQUEST["is_ajax"];
    	 $sort_by = empty($_REQUEST["sort_by"])?"t.topupid":$_REQUEST["sort_by"];
    	 $sort_order = empty($_REQUEST["sort_order"])?"DESC":$_REQUEST["sort_order"];
    	 $page = empty($_REQUEST["page"])?1:$_REQUEST["page"];
    	 
		 $this->jypage->SetAttr(array(
			  'table'=>table("topup")." as t left join ".table("users")." as u on t.userid=u.userid",
			  'pagesize'=>10,
			  'page'=>$page,
			  'key'=>'t.topupid',
		      'field'=>array('t.*','u.nickname'),
			  'where'=>"t.companyid=".$this->userdata["companyid"],
			  'condition'=>' order by '.$sort_by." ".$sort_order,
			  'pageurl'=>baseurl."orders/pricelist/?sort_by=".$sort_by."&sort_order=".$sort_order."&page=",
			  ));
		 $this->jypage->SetDb($this->db);
		
		 $pagelist = $this->jypage->pages();
		 $pagestr = $this->jypage->SetPageStr();
    	
		 $this->mytp->assign("pagestr",$pagestr);
		 $this->mytp->assign("pagelist",$pagelist["ZeroPage"]);
    	
    	 $this->mytp->assign("full_page",$is_ajax==1?0:1);

    	 if($is_ajax)
    	 {
    	 	 
    	 	 $result = array("error"=>"0","content"=>"","message"=>"");
    	 	 $result["content"] = $this->mytp->fetch("price_list.htm");
    	 	 echo json_encode($result);
    	 }else
    	 {
    	 	  $this->mytp->assign("page_title","充值记录");
    	 	  $this->mytp->tp("price_list");
    	 }
    }
    
    function finish()
    {
    	$ordersid = unserialize(base64_decode($_POST["ordersid"]));
    	if(is_array($ordersid) && count($ordersid)>0)
    	{
    		#处理订单
    		$query = $this->db->query("select foodid,foodnum,userid,foodprice from ".table("orders")." where ordersid in(".implode(",",$ordersid).")");
			$rs = $query->result_array();
			
			$price = array();
			if(is_array($rs))
			{
				foreach ($rs as $v)
				{
					@$price[$v["userid"]]+=$v["foodprice"]*$v["foodnum"];
				}
			}
			foreach ($price as $k=>$d)
			{
				$this->db->query("update ".table("users")." set balance=balance-".$d." where userid=".$k);
			}
			foreach ($ordersid as $b)
			{
				$this->db->query("update ".table("orders")." set isdo=1 where ordersid=".$b);
			}
			s("处理成功",baseurl."orders/lists");
			
    	}else
    	{
    		e("没有订单要处理");
    	}
    	
    }
    function userlist()
    {
    	$is_ajax = empty($_REQUEST["is_ajax"])?0:$_REQUEST["is_ajax"];
    	 $sort_by = empty($_REQUEST["sort_by"])?"balance":$_REQUEST["sort_by"];
    	 $sort_order = empty($_REQUEST["sort_order"])?"ASC":$_REQUEST["sort_order"];
    	 $page = empty($_REQUEST["page"])?1:$_REQUEST["page"];
    	 
		 $this->jypage->SetAttr(array(
			  'table'=>table("users"),
			  'pagesize'=>10,
			  'page'=>$page,
			  'key'=>'userid',
			  'where'=>"companyid=".$this->userdata["companyid"],
			  'condition'=>' order by '.$sort_by." ".$sort_order,
			  'pageurl'=>baseurl."orders/userlist/?sort_by=".$sort_by."&sort_order=".$sort_order."&page=",
			  ));
		 $this->jypage->SetDb($this->db);
		
		 $pagelist = $this->jypage->pages();
		 $pagestr = $this->jypage->SetPageStr();
    	
		 $this->mytp->assign("pagestr",$pagestr);
		 $this->mytp->assign("pagelist",$pagelist["ZeroPage"]);
    	
    	 $this->mytp->assign("full_page",$is_ajax==1?0:1);

    	 if($is_ajax)
    	 {
    	 	 
    	 	 $result = array("error"=>"0","content"=>"","message"=>"");
    	 	 $result["content"] = $this->mytp->fetch("user_list.htm");
    	 	 echo json_encode($result);
    	 }else
    	 {
    	 	  $this->mytp->assign("page_title","用户列表");
    	 	  $this->mytp->tp("user_list");
    	 }
    }
    function show()
    {
    	 $is_ajax = empty($_REQUEST["is_ajax"])?0:$_REQUEST["is_ajax"];
    	 $sort_by = empty($_REQUEST["sort_by"])?"o.ordersid":$_REQUEST["sort_by"];
    	 $sort_order = empty($_REQUEST["sort_order"])?"DESC":$_REQUEST["sort_order"];
    	 $page = empty($_REQUEST["page"])?1:$_REQUEST["page"];
    	 
		 $this->jypage->SetAttr(array(
			  'table'=>table("orders")." as o left join ".table("food")." as f on o.foodid=f.foodid left join ".table("users")." as u on o.userid=u.userid",
			  'pagesize'=>10,
			  'page'=>$page,
			  'key'=>'o.ordersid',
			  'where'=>"o.companyid=".$this->userdata["companyid"],
			  'field'=>array("f.foodname","o.foodnum","o.isdo","o.foodprice","o.createtime","o.ordersid","u.nickname"),
			  'condition'=>' order by '.$sort_by." ".$sort_order,
			  'pageurl'=>baseurl."orders/show/?sort_by=".$sort_by."&sort_order=".$sort_order."&page=",
			  ));
		 $this->jypage->SetDb($this->db);
		
		 $pagelist = $this->jypage->pages();
		 $pagestr = $this->jypage->SetPageStr();
    	
		 $this->mytp->assign("pagestr",$pagestr);
		 $this->mytp->assign("pagelist",$pagelist["ZeroPage"]);
    	
    	 $this->mytp->assign("full_page",$is_ajax==1?0:1);

    	 if($is_ajax)
    	 {
    	 	 
    	 	 $result = array("error"=>"0","content"=>"","message"=>"");
    	 	 $result["content"] = $this->mytp->fetch("orders_show.htm");
    	 	 echo json_encode($result);
    	 }else
    	 {
    	 	  $this->mytp->assign("page_title","订单列表");
    	 	  $this->mytp->tp("orders_show");
    	 }
    }
   
    
}    
?>